//
//  UIColor+AFF.m
//  AnyfishApp
//
//  Created by Bob Lee on 15/8/28.
//  Copyright (c) 2015年 AFF. All rights reserved.
//

#import "UIColor+AFF.h"
#import "UIImage+Orientation.h"

#define DEFAULT_VOID_COLOR [UIColor clearColor]

@implementation UIColor (AFF)

+ (NSArray *)colorsWithARGBString:(NSString *) stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];//字符串处理
    
    //例子，stringToConvert #ffffff
    if ([cString length] < 6){
        return [NSArray arrayWithObjects:DEFAULT_VOID_COLOR, nil];//如果非十六进制，返回白色
    }
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];//去掉头
    if ([cString length] != 6)//去头非十六进制，返回白色
        return [NSArray arrayWithObjects:DEFAULT_VOID_COLOR, nil];
    //分别取RGB的值
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    unsigned int r, g, b;
    //NSScanner把扫描出的制定的字符串转换成Int类型
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    //转换为UIColor
    return [NSArray arrayWithObjects:[UIColor colorWithRed:((float) r / 255.0f)
                                                     green:((float) g / 255.0f)
                                                      blue:((float) b / 255.0f)
                                                     alpha:1.0f],
            [UIColor colorWithRed:((float) (r>40 ? r-30 : r) / 255.0f)
                            green:((float) (g>40 ? g-30 : g) / 255.0f)
                             blue:((float) (b>40 ? b-30 : b) / 255.0f)
                            alpha:1.0f],
            nil];
}

+ (UIColor *)colorWithARGBString:(NSString *) stringToConvert alpha:(CGFloat)alpha{
    if([NSString isNilOrEmpty:stringToConvert])
        return DEFAULT_VOID_COLOR;
    
    stringToConvert = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];//字符串处理
    
    //例子，stringToConvert #ffffff
    if ([stringToConvert length] < 6){
        return DEFAULT_VOID_COLOR;//如果非十六进制，返回白色
    }
    if ([stringToConvert hasPrefix:@"#"])
        stringToConvert = [stringToConvert substringFromIndex:1];//去掉头
    if ([stringToConvert length] != 6)//去头非十六进制，返回白色
        return DEFAULT_VOID_COLOR;
    
    unsigned int r, g, b;
    //NSScanner把扫描出的制定的字符串转换成Int类型
    [[NSScanner scannerWithString:[stringToConvert substringWithRange:NSMakeRange(0, 2)]] scanHexInt:&r];
    [[NSScanner scannerWithString:[stringToConvert substringWithRange:NSMakeRange(2, 2)]] scanHexInt:&g];
    [[NSScanner scannerWithString:[stringToConvert substringWithRange:NSMakeRange(4, 2)]] scanHexInt:&b];
    //转换为UIColor
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:alpha];
}

+ (UIColor *)colorWithARGBString:(NSString *) stringToConvert
{
    return [UIColor colorWithARGBString:stringToConvert alpha:1.0];
}

+ (NSString*)colorToString:(UIColor*)color
{
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    int red = (int)(components[0] * 255);
    int green = (int)(components[1] * 255);
    int blue = (int)(components[2] * 255);
    //    int alpha = (int)(components[3] * 255);
    return [NSString stringWithFormat:@"#%0.2X%0.2X%0.2X", red, green, blue];
}

+ (NSDictionary*)colorStrToRGBA:(NSString*)colorStr {
    return [UIColor colorToRGBA:[UIColor colorWithARGBString:colorStr]];
}

+ (NSDictionary*)colorToRGBA:(UIColor*)color
{
    if (!color) {
        return @{@"R":@0,
                 @"G":@0,
                 @"B":@0,
                 @"A":@0};
    }
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    CGFloat red = components[0];
    CGFloat green = components[1];
    CGFloat blue = components[2];
    CGFloat alpha = components[3];
    return @{@"R":[NSNumber numberWithFloat:red],
             @"G":[NSNumber numberWithFloat:green],
             @"B":[NSNumber numberWithFloat:blue],
             @"A":[NSNumber numberWithFloat:alpha]};
}

+ (UIColor *)colorTiledWithImageName:(NSString*)imageName{
    UIImage *img = [UIImage imageWithFileName:imageName];
    UIColor *color = [UIColor colorWithPatternImage:img];
    return color;
}

// =0默认，1=水平，2=垂直
+ (UIColor *)colorTiledWithImageName:(NSString*)imageName flip:(U8)flip{
    UIImage *img = [UIImage imageWithFileName:imageName];
    
    if(flip==1){
        img = [img flipHorizontal];
    }else if(flip==2){
        img = [img flipVertical];
    }
    
    UIColor *color = [UIColor colorWithPatternImage:img];
    return color;
}

@end
